package com.huluwa.genertor;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ArrayUtil;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

/**
 * @Classname StaticGenerator
 * @Date 2024/3/2 11:07
 * @Created by hulwua
 */
public class StaticGenerator {

    public static void copyFilesByHutool(String source,String out) {
        FileUtil.copy(source,out,false);
    }
    public static void main(String[] args) {
        // 获取整个项目的根路径
        String projectPath = System.getProperty("user.dir");
        File parentFile = new File(projectPath).getParentFile();
        // 输入路径：ACM 示例代码模板目录
        String inputPath = new File(parentFile, "huluwa-generator-demo-projects/acm-template").getAbsolutePath();
        // 输出路径：直接输出到项目的根目录
        String outputPath = projectPath;
        copyFilesByHutool(inputPath, outputPath);
    }

    public static void copyFilesByRecursive(String sourcePath,String outPath) {
        File sourceFile = new File(sourcePath);
        File outFile = new File(outPath);
        try {
            copyFileByRecursive(sourceFile,outFile);
        } catch (IOException e) {
            System.out.println("文件复制失败");
            e.printStackTrace();
        }
    }
    public  static  void copyFileByRecursive(File source,File out) throws IOException {
        // 是不是目录
        if (source.isDirectory()) {
            System.out.println(source.getName());
            File destOutputFile = new File(out, source.getName());
            // 如果是目录，首先创建目标目录
            if (!destOutputFile.exists()) {
                destOutputFile.mkdirs();
            }
            // 获取目录下的所有文件和子目录
            File[] files = source.listFiles();
            // 无子文件，直接结束
            if (ArrayUtil.isEmpty(files)) {
                return;
            }
            for (File file : files) {
                // 递归拷贝下一层文件
                copyFileByRecursive(file, destOutputFile);
            }
        }else {
            // 是不是文件 是文件直接复制到目录下
            Path  destPath = out.toPath().resolve(source.getName());
            Files.copy(source.toPath(),destPath, StandardCopyOption.REPLACE_EXISTING );
        }

    }

}
